package com.flyme.module.file.provider.service.impl;


import com.flyme.common.core.constants.CommonConstants;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.file.client.constants.FileConstants;
import com.flyme.module.file.client.entity.SysLive;
import com.flyme.module.file.client.event.PoylEvent;
import com.flyme.module.file.provider.mapper.SysLiveMapper;
import com.flyme.module.file.provider.service.SysLiveService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: flyme
 * @date: 2018/3/7 15:27
 * @desc: 直播管理接口实现
 */
@Service
public class SysLiveServiceImpl extends BaseServiceImpl<SysLiveMapper, SysLive> implements SysLiveService {


    @Override
    public Boolean add(SysLive live) {
        Integer n = count();
        Boolean tag = false;
        if (n < 20) {
            live.setLiveStatus(FileConstants.LIVE_PUB);
            tag = save(live);
            applicationContext.publishEvent(new PoylEvent(this, 1L, live, null, 1));
        }
        return tag;
    }


    @Override
    public Boolean edit(SysLive live) {
        SysLive check = getById(live.getId());
        if (ObjectUtils.isEmpty(check.getChannelId())) {
            Integer n = count();
            if (n < 20) {
                applicationContext.publishEvent(new PoylEvent(this, 1L, live, null, 1));
            }
        }
        return saveOrUpdate(live);
    }

    @Override
    public ResultBody delByIds(Long[] ids) {
        List<String> channelIds = selectChannelIds(ids);
        applicationContext.publishEvent(new PoylEvent(this, null, null, channelIds, 2));
        remove(q().in("id", ids));
        return ResultBody.success("刪除成功");
    }

    /**
     * 查询在线视频
     */
    public List<EntityMap> selectLive(Integer liveStatus) {
        PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageNumber(1);
        pagerInfo.setPageSize(5);
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("id,liveTitle,liveImage,liveTime,channelId");
        cq.eq("liveStatus", liveStatus);
        cq.orderByAsc("sortOrder");
        List<EntityMap> list = (List<EntityMap>) pageList(cq).getRows();
        return list;
    }

    /**
     * 网站首页查询在线直播
     */
    public List<EntityMap> selectLiveWeb(PagerInfo pagerInfo,Integer liveStatus) {
        /*PagerInfo pagerInfo = new PagerInfo();
        pagerInfo.setPageSize(5);
        pagerInfo.setPageNumber(1);*/
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
//        queryWrapper.select("l.id,liveTitle,liveImage,liveTime,isVipFree,regFee,user.avatar lecturerImg,user.userName lecturerName,channelId");
        queryWrapper.select("l.id,liveTitle,liveImage,liveTime,isVipFree,regFee,lecturer.lecturerImg,lecturer.lecturerName,channelId");
        queryWrapper.eq("liveStatus", liveStatus);
        queryWrapper.orderByAsc("sortOrder");
//        queryWrapper.createAlias("user", "c");
        queryWrapper.createAlias("lecturer", "lecturer");
        List<EntityMap> list = (List<EntityMap>) pageList(queryWrapper).getRows();
        return list;
    }

    /**
     * 网站查询直播列表
     */
    public PqGrid selectLiveListWeb(Integer liveStatus, PagerInfo pagerInfo) {
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
//        queryWrapper.select("l.id,liveTitle,liveImage,liveTime,regFee,isVipFree,user.userName lecturerName,user.avatar lecturerImg,channelId");
        queryWrapper.select("l.id,liveTitle,liveImage,liveTime,regFee,isVipFree,lecturer.lecturerName,lecturer.lecturerImg,channelId");
        queryWrapper.eq("liveStatus", CommonConstants.ENABLED);
        queryWrapper.orderByAsc("sortOrder");
//        queryWrapper.createAlias("user", "c");
        queryWrapper.createAlias("lecturer", "lecturer");
        return pageList(queryWrapper);
    }

    /**
     * 查询一级分类下的所有二级分类
     */
    public List<String> selectChannelIds(Long[] ids) {
        return listObjs(q().select("channelId").in("id", ids), e -> e.toString());
    }

}
